package com.tjetc.impl;

import com.tjetc.dao.CommunityfeeMapper;
import com.tjetc.domian.Communityfee;
import com.tjetc.service.FeeService;
import com.tjetc.utils.Db;
import com.tjetc.vo.Result;

import java.util.List;

public class FeeServiceImpl implements FeeService {
    private CommunityfeeMapper feeMapper = Db.getMapper(CommunityfeeMapper.class);
    Result result  = new Result();
    @Override
    public List<Communityfee> select(int apartmentId) {
        return feeMapper.list( apartmentId);
    }

    @Override
    public boolean pay(int feeid, double money) {
        Communityfee fee = feeMapper.selectByPrimaryKey(feeid);
        if (fee.getAmount().doubleValue() >= money){
            fee.setPaymentdate(new java.util.Date());
            feeMapper.updateByPrimaryKey(fee);
            return true;
        }
        return false;
    }

    @Override
    public List<Communityfee> selectAll() {
        return feeMapper.selectAll();
    }

    @Override
    public Result add(Communityfee communityfee) {
       int rows= feeMapper.insert(communityfee);
      if (rows==1){
          return result.setCode(200).setMsg("添加成功").setData(communityfee);
      }else {
          return result.setCode(500).setMsg("添加失败").setData(null);
      }
    }

    @Override
    public Result updata(Communityfee communityfee) {

        int rows= feeMapper.updateByPrimaryKey(communityfee);
        if (rows==1){
            return result.setCode(200).setMsg("修改成功").setData(communityfee);
        }else {
            return result.setCode(500).setMsg("修改失败");
        }
        }


    @Override
    public Result del(int feeid) {
        if (feeMapper.selectByPrimaryKey(feeid)==null){
            return result.setCode(500).setMsg("删除内容不存在").setData(null);
        }else {
            int rows= feeMapper.deleteByPrimaryKey(feeid);
            if (rows==1){
                return result.setCode(200).setMsg("删除成功").setData(null);
            }else {
                return result.setCode(500).setMsg("删除失败").setData(null);
            }
        }

        }

    @Override
    public List<Communityfee> selectAllByDate() {
        return feeMapper.selectAllByDate();
    }
}



